
$(function () {
    getIsAndNoAssignStudent();
});

function getIsAndNoAssignStudent() {
    let data = {
        id: sessionStorage.getItem("teamId"),
    };
    let is = myAjax("/api/stu/team/getIsAssignStudent", data, 'get');
    let no = myAjax("/api/stu/team/getNoAssignStudent", data, 'get');
    if (is.code === 200) {
        setStudentData(is.data.list, no.data.list);
    }
    if (is.code === 500) {
        setStudentData([], no.data.list);
    }
}

function setStudentData(is, no) {
    let data1 = [];
    for (let i = 0; i < no.length; i++) {
        data1.push({
            value: no[i].id,
            title: no[i].nickName
        })
    }
    let data2 = [];
    for (let i = 0; i < is.length; i++) {
        data1.push({
            value: is[i].id,
            title: is[i].nickName
        });
        data2.push(is[i].id);
    }

    layui.use(['transfer', 'layer', 'util'], function () {
        var $ = layui.$
            , transfer = layui.transfer;

        //定义标题及数据源
        transfer.render({
            elem: '#test2'
            , title: ['未分配学员', '当前小组成员']  //自定义标题
            , data: data1
            //,width: 150 //定义宽度
            , height: 400 //定义高度
            , parseData: function (res) {//数据参数来源data1转换处理
                return {
                    "value": res.value //data1属性
                    , "title": res.title//data1属性
                    , "userId": res.value//data1属性
                    , "teamId": sessionStorage.getItem("teamId")
                }
            }
            , showSearch: true//显示搜索框
            , value: data2//用于回显或默认选中
            , onchange: function (data, index) {
                if (index === 0) {
                    // console.log(data, ">>>>>>>>>>>>>>"); //得到当前被穿梭的数据
                    // console.log(index); //如果数据来自左边，index 为 0，否则为 1
                    let result = teamAjax("/api/stu/tur/assignStudents", JSON.stringify(data), "post");
                    if (result.code === 200) {
                        layer.alert(result.message, {icon: 1});
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                } else {
                    // console.log(data, "<<<<<<<<<<<<<<<"); //得到当前被穿梭的数据
                    let result = teamAjax("/api/stu/tur/dels", JSON.stringify(data), "post");
                    if (result.code === 200) {
                        layer.alert(result.message, {icon: 1});
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }
            }
        });

    });
}